package com.bawei.product.controller;

import com.bawei.common.core.domain.Result;
import com.bawei.common.core.utils.poi.ExcelUtil;
import com.bawei.common.core.web.controller.BaseController;
import com.bawei.common.core.web.page.TableDataInfo;
import com.bawei.common.log.annotation.Log;
import com.bawei.common.log.enums.BusinessType;
import com.bawei.common.security.annotation.RequiresPermissions;
import com.bawei.product.domain.MallProductInfo;
import com.bawei.product.service.IMallProductInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 商品信息Controller
 *
 * @author ruoyi
 * @date 2024-05-26
 */
@RestController
@RequestMapping("/info")
public class MallProductInfoController extends BaseController
{
    @Autowired
    private IMallProductInfoService mallProductInfoService;


    /**
     * 查询商品信息列表
     */
    @RequiresPermissions("product:info:list")
    @GetMapping("/list")
    public TableDataInfo list(MallProductInfo mallProductInfo)
    {
        startPage();
        List<MallProductInfo> list = mallProductInfoService.selectMallProductInfoList(mallProductInfo);
        return getDataTable(list);
    }

    /**
     * 导出商品信息列表
     */
    @RequiresPermissions("product:info:export")
    @Log(title = "商品信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MallProductInfo mallProductInfo)
    {
        List<MallProductInfo> list = mallProductInfoService.selectMallProductInfoList(mallProductInfo);
        ExcelUtil<MallProductInfo> util = new ExcelUtil<MallProductInfo>(MallProductInfo.class);
        util.exportExcel(response, list, "商品信息数据");
    }

    /**
     * 获取商品信息详细信息
     */
    @RequiresPermissions("product:info:query")
    @GetMapping(value = "/{id}")
    public Result getInfo(@PathVariable("id") Long id)
    {
        return Result.success(mallProductInfoService.selectMallProductInfoById(id));
    }

    /**
     * 新增商品信息
     */
    @RequiresPermissions("product:info:add")
    @Log(title = "商品信息", businessType = BusinessType.INSERT)
    @PostMapping
    public Result add(@RequestBody MallProductInfo mallProductInfo)
    {
        return Result.success(mallProductInfoService.insertMallProductInfo(mallProductInfo));
    }

    /**
     * 修改商品信息
     */
    @RequiresPermissions("product:info:edit")
    @Log(title = "商品信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result edit(@RequestBody MallProductInfo mallProductInfo)
    {
        return Result.success(mallProductInfoService.updateMallProductInfo(mallProductInfo));
    }

    /**
     * 删除商品信息
     */
    @RequiresPermissions("product:info:remove")
    @Log(title = "商品信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public Result remove(@PathVariable Long[] ids)
    {
        return Result.success(mallProductInfoService.deleteMallProductInfoByIds(ids));
    }

    /**
     * 上传图片
     * @param
     * @return
     */
    @PostMapping("/upload")
    public Result upload(@RequestParam("file")MultipartFile file)
    {
        Result result=mallProductInfoService.uploadMallProductInfoByIds(file);
        return Result.success(result);
    }

    /**
     * 上架
     */
    @PostMapping("/upFlag")
    public Result upFlag(@RequestParam Integer id){
        Result result=mallProductInfoService.upFlag(id);
        return result;
    }

    /**
     * 下架
     */
    @PostMapping("/downFlag")
    public Result downFlag(@RequestParam Integer id){
        Result result=mallProductInfoService.downFlag(id);
        return result;
    }
}
